package com.t114514.demo.service.impl;

import com.t114514.demo.mapper.QuestionnaireAnswerMapper;
import com.t114514.demo.pojo.Answer;
import com.t114514.demo.pojo.StudentQuestionnaireTeacher;
import com.t114514.demo.service.QuestionnaireAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


@Service
public class QuestionnaireAnswerServiceImpl implements QuestionnaireAnswerService {

    @Autowired
    private QuestionnaireAnswerMapper questionnaireAnswerMapper;
    @Override
    public void add(StudentQuestionnaireTeacher studentQuestionnaireTeacher) {
        studentQuestionnaireTeacher.setIsValidate(0);
        questionnaireAnswerMapper.addStudentQuestionnaireTeacher(studentQuestionnaireTeacher);
    }

    @Override
    public void addAnswer(Answer answer) {
        answer.setIsValidate(0);
        questionnaireAnswerMapper.addAnswer(answer);

    }

    @Override
    public void finishAnswer(@RequestBody StudentQuestionnaireTeacher studentQuestionnaireTeacher) {
        //将学生所回答的问题的isvalidate改为1
        questionnaireAnswerMapper.updateAllAnswer(studentQuestionnaireTeacher);
        //同时将该问卷的isvalidate改为1
        questionnaireAnswerMapper.updateStudentQuestionnaireTeacher(studentQuestionnaireTeacher);
    }


    @Override
    public Answer isExist(Answer answer) {
        answer.setIsValidate(0);
        return questionnaireAnswerMapper.isExist(answer);
    }

    @Override
    public void updateAnswer(Answer answer) {
        questionnaireAnswerMapper.updateAnswerContent(answer);
    }

    @Override
    public boolean answerAllQuestion(StudentQuestionnaireTeacher studentQuestionnaireTeacher) {

        int questionNum=questionnaireAnswerMapper.getQuestionNum(studentQuestionnaireTeacher);
        int answerNum=questionnaireAnswerMapper.getAnswerNum(studentQuestionnaireTeacher);
        if (questionNum==answerNum){
            return true;
        }
        return false;
    }

    @Override
    public List<Answer> findAnswersByQuestionId(Integer questionId,int teacherId) {
        return questionnaireAnswerMapper.selectAnswerByQuestionId(questionId,teacherId);
    }
}
